package com.boot.unity.api.modules.province.service.impl;

import com.boot.unity.api.modules.province.result.Result;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.boot.unity.api.modules.province.service.TrainingTimeService;
import com.boot.unity.api.modules.province.dao.TrainingTimeDao;
import com.boot.unity.api.modules.province.entity.TrainingTime;

import java.util.List;

/**
 * @author anonymous
 *
 * @create 2020-08-18 21:42:02
 */
@Service
@AllArgsConstructor
public class TrainingTimeServiceImpl implements TrainingTimeService {
    private final TrainingTimeDao trainingTimeDao;

    @Override
    public Result save(TrainingTime trainingTime) {
        String sfzmhm = trainingTime.getSfzmhm();
        Integer pxkm = trainingTime.getPxkm();
        String pxcx = trainingTime.getPxcx();
        Integer origin = trainingTime.getOrigin();
        if (StringUtils.isBlank(sfzmhm)){
            return Result.fail("学员身份证明号码不能为空!");
        }
        if (pxkm == null){
            return Result.fail("培训科目不能为空!");
        }
        if (StringUtils.isBlank(pxcx)){
            return Result.fail("培训车型不能为空!");
        }
        TrainingTime params = new TrainingTime();
        params.setSfzmhm(sfzmhm);
        params.setPxkm(pxkm);
        params.setPxcx(pxcx);
        params.setOrigin(origin);
        TrainingTime tt = trainingTimeDao.getTrainingTime(params);
        if (tt == null){
            trainingTimeDao.save(trainingTime);
        }else {
            trainingTimeDao.update(trainingTime);
        }


        //进行处理插入重复的问题
        List<TrainingTime> list = trainingTimeDao.queryColumnMore();
        for(TrainingTime entity:list){
            int id = trainingTimeDao.findByspp(entity.getSfzmhm(),entity.getPxkm(),entity.getPxcx());
            //进行更新多余的数据
            trainingTimeDao.updateMoreColumn(id);
        }

        return Result.success();
    }

    @Override
    public Result list() {
        List<TrainingTime> trainingTimeList = trainingTimeDao.list10();
        return Result.success("获取数据成功!",trainingTimeList);
    }
}
